package com.simple.datax.flow.preprocessor.recvprotocol;

import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.constants.TaskActivityConstants;
import com.simple.datax.flow.convertor.Convertor;
import com.simple.datax.flow.dao.ibatis.model.FlowPreProcessor;
import com.simple.datax.flow.dao.ibatis.model.TaskDec;
import com.simple.datax.flow.message.BaseEcMessage;
import com.simple.datax.flow.message.EcFlowMessage;
import com.simple.datax.flow.message.EcFlowRespMessage;
import com.simple.datax.flow.message.EcFlowRespMessageBody.ResponseActionResult;
import com.simple.datax.flow.service.PreProcessorManager;
import com.simple.datax.flow.service.TaskManager;
import com.simple.datax.flow.service.TaskMessageManager;

/** 
 * @Title: TcsFlow201ResponseProtocol.java 
 * @Package com.simple.datax.flow.preprocessor.recvprotocol 
 * @Description: TODO(接收到tcs回执后生成原始报文发送给货主协议实现) 
 * @author tonney 
 * @date 2013-1-25 上午10:18:33 
 * @version V1.0 
 */
public class TcsFlow201ResponseProtocol implements RecvProtocolIF{
	public Logger log = LoggerFactory.getLogger(this.getClass());
	private PreProcessorManager preProcessorManager;
	
	private TaskMessageManager taskMessageManager;

	private Convertor messageConvertor;
	
	private TaskManager taskManager;
	
	public void setTaskManager(TaskManager taskManager) {
		this.taskManager = taskManager;
	}

	public void setMessageConvertor(Convertor messageConvertor) {
		this.messageConvertor = messageConvertor;
	}

	public void setTaskMessageManager(TaskMessageManager taskMessageManager) {
		this.taskMessageManager = taskMessageManager;
	}

	public void setPreProcessorManager(PreProcessorManager preProcessorManager) {
		this.preProcessorManager = preProcessorManager;
	}

	public int process(BaseEcMessage ecMessage,
			List<BaseEcMessage> ecMessageList) {
		int result = ProcessResultConstants.STATUS_SUCCESS;
		try{
			EcFlowRespMessage ecFlowRespMessage = (EcFlowRespMessage)ecMessage;
			FlowPreProcessor preProcessor = null;
//			EcFlowMessage ecFlowMessage = null;
			String ecode = ecFlowRespMessage.getFlowRespMessageHead().getSenderId();
			String msgType = ecFlowRespMessage.getFlowRespMessageHead().getMessageType();
			String activityNo = ecFlowRespMessage.getFlowRespMessageBody().getActionResultList().get(0).getActionId();
			String resultcode = ecFlowRespMessage.getFlowRespMessageBody().getActionResultList().get(0).getResultCode();
			String resultValue = ecFlowRespMessage.getFlowRespMessageBody().getActionResultList().get(0).getResultValue();
//			if(resultcode!=null&&resultcode.equals("0")){   //tcs回执成功
				String taskid = ecFlowRespMessage.getFlowRespMessageBody().getEcFlowResponseHead().getTaskId();
				String flowid = taskManager.getFlowidByTaskid(taskid);
				if(flowid!=null){
					preProcessor = preProcessorManager.getFlowPreProcessor(ecode,msgType,activityNo,flowid);
					if(preProcessor!=null){  //有预处理器
						/**********************************给货主转发原始报文 BEGIN****************************************************************/
//						TaskDec taskDec = taskMessageManager.getTaskDecByTaskid(taskid);
//						if(taskDec!=null){
//							BaseEcMessage baseEcMessage = messageConvertor.convert2EcMessage(new String(taskDec.getDecMsg(),"utf-8"));
//							if(baseEcMessage!=null){
//								ecFlowMessage = (EcFlowMessage)baseEcMessage;
								/**********************************给货主转发原始报文 END*****************************************************************/
								String toActivitys = preProcessor.getTransActivityNo();
								String[] activityArray = toActivitys.split(TaskActivityConstants.ACTIVITY_SEPRATOR);
								if(activityArray.length>0){
									ResponseActionResult actionResult = null;
									for(String actNo:activityArray){
										EcFlowRespMessage retEcFlowRespMessage = ecFlowRespMessage.clone();
										retEcFlowRespMessage.getFlowRespMessageBody().getActionResultList().clear();
//										retEcFlowRespMessage = new EcFlowRespMessage();
//										retEcFlowRespMessage.setFlowRespMessageHead(ecFlowRespMessage.getFlowRespMessageHead());
//										retEcFlowRespMessage.setFlowRespMessageBody(ecFlowRespMessage.getFlowRespMessageBody());
//										retEcFlowRespMessage = ecFlowRespMessage.clone();
										actionResult = retEcFlowRespMessage.getFlowRespMessageBody().new ResponseActionResult();
										actionResult.setActionId(actNo);
										actionResult.setResultCode(resultcode);
										actionResult.setResultValue(resultValue);
										retEcFlowRespMessage.getFlowRespMessageBody().getActionResultList().add(actionResult);
										retEcFlowRespMessage.setValidActivityNo(false);
										retEcFlowRespMessage.getFlowRespMessageBody().getEcFlowResponseHead().setBpNo(flowid);
										ecMessageList.add(retEcFlowRespMessage);
									}
								}
//							}else{
//								log.error("TcsFlow201ResponseProtocol::process DEC convert to EcMessage failure");
//							}
//						}else{
//							result = ProcessResultConstants.STATUS_RECV_PROCESS_EXCEPTION;
//							log.error("TCSPreProcessorImpl::recvPreProcessor error,Can not get TaskDec by taskid=>"+taskid);
//							return result;
//						}
					}
				}else{
					result = ProcessResultConstants.STATUS_RECV_PROCESS_EXCEPTION;
					log.error("TCSPreProcessorImpl::recvPreProcessor can not find flowid by taskid["+taskid+"]");
				}
//			}else{  //给报关行发送错误回执,暂不作处理直接推送给企业
//				
//			}			
		}catch(Exception e){
			log.error("TcsFlow201ResponseProtocol::process error=>"+e.toString());
		}
		return result;
	}
}
